package com.carsystem.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.carsystem.entity.Member;
import com.carsystem.request.AliPayBean;
import com.carsystem.request.FrontOrderRequestVo;
import com.carsystem.request.OpenRendRequestVo;
import com.carsystem.request.SetOrderRequestVO;
import com.carsystem.response.FrontOrderResponseVo;
import com.carsystem.response.OpenRendResponseVo;
import com.carsystem.service.ICarService;
import com.carsystem.service.IMemberService;
import com.carsystem.service.IOpenrendService;
import com.carsystem.service.IOrderService;
import com.carsystem.utils.*;
import org.apache.catalina.session.StandardSession;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/carOrder")
public class CarOrderController extends BaseController {

    @Reference
    private IOpenrendService openrendService;
    @Reference
    private ICarService carService;
    @Reference
    private IOrderService orderService;
    @Reference
    private IMemberService memberService;

    /**
     * 前端页面展示车辆列表
     * @param openRendRequestVo
     * @return
     */
    @RequestMapping("/selectListByOpenRend")
    public ResultEntity selectListByOpenRend(@RequestBody OpenRendRequestVo openRendRequestVo){
        try {
            Map<String, Object> returnMap = new HashMap<>();
            PageHandler pageHandler = openrendService.selectOpenRendByPage(openRendRequestVo);
            returnMap.put("page",pageHandler);
            return getSuccessEntity(returnMap);
        } catch (Exception e) {
            logger.info("----------系统错误，错误原因：{}----------", e.getMessage());
            e.printStackTrace();
            return getErrorEntity(e.getMessage());
        }
    }

    /**
     * 车辆展示详情页面
     * @param id
     * @return
     */
    @RequestMapping("/selectOpenRentdById")
    public ResultEntity selectOpenRentdById(Integer id){
        try {
            OpenRendResponseVo openRendResponseVo = openrendService.selectOpenRentdById(id);
            return getSuccessEntity(openRendResponseVo);
        } catch (Exception e) {
            logger.info("----------系统错误，错误原因：{}----------", e.getMessage());
            e.printStackTrace();
            return getErrorEntity(e.getMessage());
        }
    }

    /**
     * 提交订单
     * @param setOrderRequestVO
     * @return
     */
    @RequestMapping("/setOrder")
    public Result setOrder(@RequestBody SetOrderRequestVO setOrderRequestVO){
        boolean b = orderService.setOrder(setOrderRequestVO);
        if (b){
            return new Result(true,"下单成功",b);
        }
        return new Result(false,"下单失败,请联系管理员");
    }

    /**
     * 验证手机号
     * @param phoneNumber
     * @return
     */
    @RequestMapping("/selectPhone/{phoneNumber}")
    public Result selectPhone(@PathVariable("phoneNumber") String phoneNumber){
        Member member = memberService.selectPhone(phoneNumber);
        if (member!=null){
            return new Result(true,"手机号已经存在");
        }
        return new Result(false,"手机号未注册，可以注册！");
    }

    /**
     * 注册
     * @param member
     * @return
     */
    @RequestMapping("/registerMember")
    public Result registerMember(@RequestBody Member member){
        return memberService.addMember(member);
    }

    /**
     * 登录
     */
    @RequestMapping("/loginMember")
    public Result loginMember(String phoneNumber,String password){
         Member member = memberService.loginMember(phoneNumber,password);
         if (member!=null){
             return new Result(true,"登录成功",member);
         }
         return new Result(false,"登录失败请检查是否注册，或者账号密码错误！");
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session =request.getSession();
        session.invalidate();
        Result result = new Result(true, "退出成功", null);
        String s = JSON.toJSONString(result);
        response.getWriter().println(s);
    }

    @RequestMapping("/pay")
    @ResponseBody
    public String alipay(String outTradeNo, String subject, String totalAmount, String body) {

        logger.info("商户订单号为{},订单名称为{},付款金额为{},商品描述{}", outTradeNo, subject, totalAmount, body);
        AliPayBean alipayBean = new AliPayBean();
        alipayBean.setOut_trade_no(outTradeNo);
        alipayBean.setSubject(subject);
        alipayBean.setTotal_amount(totalAmount);
        alipayBean.setBody(body);

        return orderService.aliPay(alipayBean);
    }

    @RequestMapping("/success")
    @ResponseBody
    public String success(){

        return "交易成功！";
    }

    @RequestMapping(value = "/index")
    public String payCoin(){
        return "index.html";
    }

    @RequestMapping("/selectOrderById")
    public ResultEntity selectOrderById(@RequestBody FrontOrderRequestVo frontOrderRequestVo){
        try {
            Map<String, Object> returnMap = new HashMap<>();
            PageHandler pageHandler=orderService.selectOrderByIdList(frontOrderRequestVo);
            returnMap.put("page",pageHandler);
            return getSuccessEntity(returnMap);
        } catch (Exception e) {
            logger.info("----------系统错误，错误原因：{}----------", e.getMessage());
            e.printStackTrace();
            return getErrorEntity(e.getMessage());
        }
    }
}
